﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Card.Utility
{
    public class ControlUtility
    {
        public static void SelectItemByValue(ListControl ltc, string value)
        {
            if (ltc.Items.Count > 0)
            {
                ltc.SelectedIndex = -1;
                ListItem lst = ltc.Items.FindByValue(value);
                if (lst != null)
                {
                    ltc.SelectedIndex = ltc.Items.IndexOf(lst);
                }
            }
        }

        public static void BindListControl<T>(ListControl ltc, List<T> lst, string name, string value)
        {
            ltc.DataSource = lst;
            ltc.DataValueField = value;
            ltc.DataTextField = name;
            ltc.DataBind();
        }
        /// <summary>
        /// 是否在第一位插入"----全部----"
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ltc"></param>
        /// <param name="lst"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="insertAll"></param>
        public static void BindListControl<T>(ListControl ltc, List<T> lst, string name, string value,bool insertAll)
        {
            BindListControl<T>(ltc, lst, name, value);
            if (insertAll)
            {
                ltc.Items.Insert(0, new ListItem("----全部----",""));
            }
        }
        
    }
}
